package dolf.objects;

import java.awt.Graphics2D;
import java.awt.Image;

import dolf.objects.WorldObject;

public class Sand extends CircleObject implements java.io.Serializable, GroundObject{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	transient private Image img;
	private double res; //ablenkung pro 1 laengen einheit 
	private int rad;
	public Sand(Point p, int r){
		super(p,r,false,true);
		res = 0.00025;
		rad = r;
		img = ImageStore.getInstance().getImage("dolf/gfx/environment/sand.png");
		img = img.getScaledInstance(2*rad, 2*rad, Image.SCALE_SMOOTH);
	}
	public Sand(Point p, int r, double _slope){
		super(p,r,false,true);
		res = _slope;
		rad = r;
		img = ImageStore.getInstance().getImage("dolf/gfx/environment/sand.png");
		img = img.getScaledInstance(2*rad, 2*rad, Image.SCALE_SMOOTH);
	}
	public void paint(Graphics2D g){
		if (img == null) {
			img = ImageStore.getInstance().getImage("dolf/gfx/environment/sand.png");
			img = img.getScaledInstance(2*rad, 2*rad, Image.SCALE_SMOOTH);
		}
		g.drawImage(img, this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	public boolean newMotion(WorldObject object){
		System.err.println("keine Kollision mit GroundObjecten");
		return true;
	}
	
	public double MoveOver(WorldObject object, double percent){
		CircleObject obj;
		if(object instanceof CircleObject && object.isMovable()){
			obj = (CircleObject) object;
		} else {
			return -1;
		}
		double len = object.getMotion().getLength()*percent;
		double len2 = len;
		//System.out.println("MoveOver "+len);
		//this.res = 0.001;
//		boolean inside = false;
	//	double speed = object.getMotion().getLength();
		while(len>=0.01){
				if(!isInside(obj)){
		//			obj.setPosition(obj.getPosition().add(obj.getMotion().setLength(0.01)));
				//	System.out.println("nicht innen geht raus "+ inside);
		//			if(inside){
						return (len/len2)*percent;
			//		}
				} else {				
			//		inside = true;
					obj.setMotion(obj.getMotion().sub(obj.getMotion().setLength(this.res)));
					obj.setPosition(obj.getPosition().add(obj.getMotion().setLength(0.01)));
				/*	if(speed > object.getMotion().getLength()){
						System.out.print("-");
					} else{
						System.out.print("+");
					}*/
				}
				len -=0.01;
		}
		return 0;
	}
	public boolean isInside(CircleObject obj){
		//System.out.println(obj.getPosition().getDist(this.getPosition()));
		if(obj.getPosition().getDist(this.getPosition())>=(this.getRadius()+obj.getRadius()+0.5)){
			return false;
		}else{
			return true;
		}
	}
}
